package com.chaoge.service.store.impl;

import com.chaoge.dao.store.QuestionItemDao;
import com.chaoge.domain.store.QuestionItem;
import com.chaoge.factory.MapperFactory;
import com.chaoge.service.store.QuestionItemService;
import com.chaoge.service.store.QuestionService;
import com.chaoge.utils.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {
    @Override
    public void save(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            //添加随机id
            String id = UUID.randomUUID().toString();
            questionItem.setId(id);
            questionItemDao.save(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void delete(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            questionItemDao.delete(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            questionItemDao.update(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public QuestionItem findById(String id) {
        SqlSession sqlSession = null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            return questionItemDao.findById(id);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public PageInfo findAll(String questionId, int page, int size) {

        SqlSession sqlSession = null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            PageHelper.startPage(page, size);
            List<QuestionItem> all = questionItemDao.findAll(questionId);
            PageInfo pageInfo = new PageInfo(all);
            return pageInfo;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}